package cqie.personal.frame.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import cqie.personal.frame.bo.GetCourseClassBO;
import cqie.personal.frame.entity.CourseClassView;
import cqie.personal.frame.exception.ValidationFailException;
import cqie.personal.frame.mapper.CourseClassViewMapper;
import cqie.personal.frame.service.CourseClassViewService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cqie.personal.frame.vo.PageVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * VIEW 服务实现类
 * </p>
 *
 * @author Yeeeeee
 * @since 2021-10-11
 */
@Service
public class CourseClassViewServiceImpl extends ServiceImpl<CourseClassViewMapper, CourseClassView> implements CourseClassViewService {

    @Resource
    private CourseClassViewMapper courseClassViewMapper;

    @Override
    public PageVO<List<CourseClassView>> getCourseClass(GetCourseClassBO getBO) throws ValidationFailException {
        if(getBO ==null){
            throw new ValidationFailException("服务器错误，未成功获取请求，请联系管理员");
        }
        if(getBO.getClassId()==null) {
            throw new ValidationFailException("未指定查询目标");
        }
        QueryWrapper<CourseClassView> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("class_id",getBO.getClassId());
        if(getBO.getCourseName()!=null){
            queryWrapper.like("course_name",getBO.getCourseName());
        }
        if(getBO.getTeacherName()!=null){
            queryWrapper.like("user_name",getBO.getTeacherName());
        }
        int totalCount = courseClassViewMapper.selectCount(queryWrapper);
        if(totalCount==0){
            return null;
        }
        queryWrapper.last(" LIMIT "+getBO.getPageOffset()+" , " + getBO.getPageSize());
        PageVO<List<CourseClassView>> result = new PageVO<>();
        List<CourseClassView> data = courseClassViewMapper.selectList(queryWrapper);
        result.setData(data);
        result.setTotalCount(totalCount);
        result.setRecordCount(data.size());
        result.setPageSize(getBO.getPageSize());
        result.setPageNo(getBO.getPageNo());
        result.setTotalPage(
                totalCount%getBO.getPageSize()==0?
                        totalCount/getBO.getPageSize():
                        (totalCount/getBO.getPageSize())+1
        );
        return result;
    }
}
